/*
 * EarGuitarTrainer.cpp
 *
 *  Created on: Jun 9, 2010
 *      Author: Mario Rodriguez Huizar
 */
#include "EarGuitarTrainer.h"
#include <Phonon/AudioOutput>
#include <QtGui>
#include <iostream>

EarGuitarTrainer::EarGuitarTrainer(QWidget *parent)
: QWidget(parent), sounds(3)
{
	// the media object knows how to playback multimedia (in our case sound files)
	// while the audio output can send it to a sound device.
	audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
	mediaObject = new Phonon::MediaObject(this);

	connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
	connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
			this, SLOT(stateChanged(Phonon::State,Phonon::State)));
	connect(mediaObject, SIGNAL(finished()), this, SLOT(finished()));

	// connect our media object to our audio output
	Phonon::createPath(mediaObject, audioOutput);

	setupUi(this);
	timeLcd->display("00:00");
	// time interval in milliseconds between two ticks.
	//The tick() signal is emitted continuously during playback. The tick interval is
	//the time that elapses between the emission of two tick signals. If you set the interval
	//to 0 the tick signal gets disabled.
	//The tick() signal can, for instance, be used to update widgets that show the
	//current position in the playback of a media source.
	mediaObject->setTickInterval(50);
	seekSlider->setMediaObject(mediaObject); //tell our slider which media to control
	volumeSlider->setAudioOutput(audioOutput); //same with our volume slider
	sounds[0].name = "A";
	sounds[0].fileName = "./sounds/A.ogg";
	sounds[0].pCheckBox = cbA;
	sounds[0].pButton = pbA;
	sounds[1].name = "E";
	sounds[1].fileName = "./sounds/E.ogg";
	sounds[1].pCheckBox = cbE;
	sounds[1].pButton = pbE;
	sounds[2].name = "D";
	sounds[2].fileName = "./sounds/D.ogg";
	sounds[2].pCheckBox = cbD;
	sounds[2].pButton = pbD;

	srand((unsigned)time(0));

	pbA->setHidden(true);
	pbE->setHidden(true);
	pbD->setHidden(true);

	randomChord = 0;
}

void EarGuitarTrainer::on_pbStop_clicked(){
	QIcon icon;
	mediaObject->stop();
	icon.addFile(QString::fromUtf8("icons/Play.tiff"), QSize(), QIcon::Normal, QIcon::Off);
	pbPlay->setIcon(icon);
}

void EarGuitarTrainer::on_pbPlay_clicked(){
	QIcon icon;
	if(mediaObject->state() == Phonon::PlayingState){
		mediaObject->pause();
		icon.addFile(QString::fromUtf8("icons/Play.tiff"), QSize(), QIcon::Normal, QIcon::Off);
		pbPlay->setIcon(icon);
	} else if(mediaObject->state() == Phonon::StoppedState){
		mediaObject->play();
		icon.addFile(QString::fromUtf8("icons/Pause.tiff"), QSize(), QIcon::Normal, QIcon::Off);
		pbPlay->setIcon(icon);
	} else if(mediaObject->state() == Phonon::PausedState){
		mediaObject->play();
		icon.addFile(QString::fromUtf8("icons/Pause.tiff"), QSize(), QIcon::Normal, QIcon::Off);
		pbPlay->setIcon(icon);
	}

}

void EarGuitarTrainer::on_bgAnswers_buttonClicked(QAbstractButton * button){
	cout << "clicked" << endl;

}

//The media object's signal is connected to this method. This method will be called
//everytime the music change its state... play, stop, pause etc..
void EarGuitarTrainer::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
{
	switch (newState) {
	case Phonon::ErrorState:
		if (mediaObject->errorType() == Phonon::FatalError) { //not possible to play the same file again
			QMessageBox::warning(this, tr("Fatal Error"),
					mediaObject->errorString()); //gives a description of the error
		} else { //it could be possible to play the same file again (not a fatal error)
			QMessageBox::warning(this, tr("Error"),
					mediaObject->errorString());
		}
		break;
	case Phonon::PlayingState:
		pbStop->setEnabled(true);
		break;
	case Phonon::StoppedState:
		pbStop->setEnabled(false);
		timeLcd->display("00:00");
		break;
	case Phonon::PausedState:
		pbStop->setEnabled(true);
		break;
	case Phonon::BufferingState:
		break;
	default:
		;
	}
}

//The tick() slot is connected to a MediaObject signal which is emitted
//when the playback position changes
void EarGuitarTrainer::tick(qint64 time)
{
	QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
	timeLcd->display(displayTime.toString("mm:ss"));
}

//read which sounds the user selected
//choose a random sound
//play it
void EarGuitarTrainer::on_pbHitIt_clicked(){
	bool atLeastOneSelected = false;


	for(unsigned int i=0; i<sounds.size(); i++){
		if(sounds[i].pCheckBox->isChecked()){
			atLeastOneSelected = true;
			break;
		}
	}
	if(true == atLeastOneSelected){
		randomChord = (rand()%sounds.size());
		while( false == sounds[randomChord].pCheckBox->isChecked() ){
			randomChord = (rand()%sounds.size());
		}
		mediaObject->setCurrentSource(sounds[randomChord].fileName);
		mediaObject->stop();
		mediaObject->clearQueue();
		mediaObject->play();
		pbPlay->setEnabled(true);
	}else{ // the user did not chose any sound to play
		QMessageBox::warning(this, tr("Ear Guitar Trainer"),
				tr("You have not selected any Chord to play.\n"
						"Please select with the checkboxes the chords to play"),
						QMessageBox::Ok);
	}
}

//Emitted when the object has finished playback. It is not emitted if you
//call stop(), pause() or load(). It is emitted only when the current media
//source has finished playing and the media queue() is empty, or when a fatal error occurs.
void EarGuitarTrainer::finished(){
	QIcon icon;

	//change the icon to display a play button instead of a pause button.
	icon.addFile(QString::fromUtf8("icons/Play.tiff"), QSize(), QIcon::Normal, QIcon::Off);
	pbPlay->setIcon(icon);
	mediaObject->stop();
	mediaObject->clearQueue();
	//show the possible answers
	for(unsigned int i=0; i<sounds.size(); i++){
		if(sounds[i].pCheckBox->isChecked())
			sounds[i].pButton->setHidden(false);
		else
			sounds[i].pButton->setHidden(true);
	}
}

EarGuitarTrainer::~EarGuitarTrainer()
{

}
